package com.jetpackframework.http;

import androidx.lifecycle.Lifecycle;

import com.gwm.annotation.http.HTTP;
import com.zhy.http.okhttp.builder.GetBuilder;
import com.zhy.http.okhttp.builder.PostFormBuilder;

import okhttp3.Request;
import okhttp3.WebSocketListener;

public class HttpTask<T> {
    private Lifecycle lifecycle;
    private String url;
    private HTTP.WAY method;
    private Request request;
    private PostFormBuilder postFormBuild;
    private GetBuilder getBuilder;
    private HttpObserver<T> observer; //控制器，http请求会将服务器返回的数据传递给该接口
    private Class<T> result;
    private HttpNetListener netListener;  //网络请求对话框
    private WebSocketListener webSocketListener;
    private String message;                 //网络消息对话框提示信息
    private boolean isStream = false;
    private String streamPath;

    private HttpTask(String url, HTTP.WAY method, Request request, PostFormBuilder postFormBuild, GetBuilder getBuilder, HttpObserver<T> observer, Class<T> result, HttpNetListener netListener, WebSocketListener webSocketListener, String message, boolean isStream, String streamPath, Lifecycle lifecycle) {
        this.url = url;
        this.method = method;
        this.request = request;
        this.postFormBuild = postFormBuild;
        this.getBuilder = getBuilder;
        this.observer = observer;
        this.result = result;
        this.netListener = netListener;
        this.webSocketListener = webSocketListener;
        this.message = message;
        this.isStream = isStream;
        this.streamPath = streamPath;
        if (netListener == null){
            this.netListener = new HttpNetListener();
        }
        this.lifecycle = lifecycle;
    }

    public String getUrl() {
        return url;
    }

    public HTTP.WAY getMethod() {
        return method;
    }

    public Request getRequest() {
        return request;
    }

    public PostFormBuilder getPostFormBuild() {
        return postFormBuild;
    }

    public GetBuilder getGetBuilder() {
        return getBuilder;
    }

    public HttpObserver<T> getObserver() {
        return observer;
    }

    public Class<T> getResult() {
        return result;
    }

    public HttpNetListener getNetListener() {
        return netListener;
    }

    public WebSocketListener getWebSocketListener() {
        return webSocketListener;
    }

    public String getMessage() {
        return message;
    }

    public boolean isStream() {
        return isStream;
    }

    public String getStreamPath() {
        return streamPath;
    }

    public Lifecycle getLifecycle() {
        return lifecycle;
    }

    public static class Builder<T>{
        private Lifecycle lifecycle;
        private String url;
        private HTTP.WAY method;
        private Request request;
        private PostFormBuilder postFormBuild;
        private GetBuilder getBuilder;
        private HttpObserver<T> observer; //控制器，http请求会将服务器返回的数据传递给该接口
        private Class<T> result;
        private HttpNetListener netListener;  //网络请求对话框
        private WebSocketListener webSocketListener;
        private String message;                 //网络消息对话框提示信息
        private boolean isStream = false;
        private String streamPath;

        public Builder setUrl(String url) {
            this.url = url;
            return this;
        }

        public Builder setMethod(HTTP.WAY method) {
            this.method = method;
            return this;
        }

        public Builder setRequest(Request request) {
            this.request = request;
            return this;
        }

        public Builder setPostFormBuild(PostFormBuilder postFormBuild) {
            this.postFormBuild = postFormBuild;
            return this;
        }

        public Builder setGetBuilder(GetBuilder getBuilder) {
            this.getBuilder = getBuilder;
            return this;
        }

        public Builder setObserver(HttpObserver<T> observer) {
            this.observer = observer;
            return this;
        }

        public Builder setResult(Class<T> result) {
            this.result = result;
            return this;
        }

        public Builder setNetListener(HttpNetListener netListener) {
            this.netListener = netListener;
            return this;
        }

        public Builder setWebSocketListener(WebSocketListener webSocketListener) {
            this.webSocketListener = webSocketListener;
            return this;
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        public Builder setStream(boolean stream) {
            isStream = stream;
            return this;
        }

        public Builder setStreamPath(String streamPath) {
            this.streamPath = streamPath;
            return this;
        }

        public HttpTask<T> build(){
            return new HttpTask<T>(url,method,request,postFormBuild,getBuilder,observer,result,netListener,webSocketListener,message,isStream,streamPath,lifecycle);
        }
        public Builder setLifecycle(Lifecycle lifecycle){
            this.lifecycle = lifecycle;
            return this;
        }
    }

    public static class HttpNetListener{

        /**
         * UI Thread
         *response.headers()
         * @param request
         */
        void onBefore(Request request, int id){

        }

        /**
         * UI Thread
         *
         * @param
         */
        void onAfter(int id){

        }

        /**
         * UI Thread
         *
         * @param progress
         */
        void inProgress(float progress, long total, int id){

        }
    }

    @Override
    public String toString() {
        return "HttpTask{" +
                "url='" + url + '\'' +
                ", method=" + method +
                ", request=" + request +
                ", postFormRequest=" + postFormBuild +
                ", getBuilder=" + getBuilder +
                ", observer=" + observer +
                ", result=" + result +
                ", netListener=" + netListener +
                ", webSocketListener=" + webSocketListener +
                ", message='" + message + '\'' +
                '}';
    }
}
